<?php
$lang = 'en';
$inputCompanyName = ($lang == "en") ? 'Company name' : 'ชื่อบริษัท';
$inputAddress = ($lang == "en") ? 'Address ' : 'ที่อยู่';
$inputProvince = ($lang == "en") ? 'Province' : 'จังหวัด';
$inputZipCode = ($lang == "en") ? 'Post code' : 'รหัสไปรษณีย์';
$inputEmail = ($lang == "en") ? 'Email (username)' : 'อีเมล(ใช้สำหรับ login)';
$inputPhone = ($lang == "en") ? 'Telephone' : 'เบอร์โทรศัพท์';
$inputFax = ($lang == "en") ? 'Fax' : 'แฟกซ์';
$inputMobile = ($lang == "en") ? 'Mobile' : 'เบอร์โทรศัพท์มือถือ';
$inputContactName = ($lang == "en") ? 'Contact Name' : 'ชื่อผู้ติดต่อ';
?>
<h1 class="th"><?= ($lang == "en") ? 'Company profile' : 'ข้อมูลบริษัท'; ?></h1>
<!-- begin job list -->
<div id="CustomerForm">
    <form class="form-horizontal" action="index.php?r=ClientManage/Save" method="post">
        <input type="hidden" name="ClientID" value="<?= isset($dataClient['ClientID'])?$dataClient['ClientID']:'' ?>">
        <div class="control-group">
            <label class="control-label required" for="inputContactName"><?= $inputContactName ?></label>
            <div class="controls">
                <input type="text" name="ContactName" id="inputContactName" placeholder="" required value="<?= isset($dataClient['ContactName'])?$dataClient['ContactName']:'' ?>">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label required" for="inputCompanyName"><?= $inputCompanyName ?></label>
            <div class="controls">
                <input type="text" name="CompanyName" id="inputCompanyName" placeholder="" required value="<?= isset($dataClient['CompanyName'])?$dataClient['CompanyName']:'' ?>">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="inputAddress"><?= $inputAddress ?></label>
            <div class="controls">
                <textarea name="Address" id="inputAddress" rows="3"><?= isset($dataClient['Address'])?$dataClient['Address']:'' ?></textarea>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="inputProvince"><?= $inputProvince ?></label>
            <div class="controls">
                <select name="Province" id="inputProvince">
                    <?= GM::getOptionDropdownProvince($lang) ?>
                </select>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label required" for="inputZipCode"><?= $inputZipCode ?></label>
            <div class="controls">
                <input type="text" name="ZipCode" id="inputZipCode" placeholder="" maxlength="5" required value="<?= isset($dataClient['ZipCode'])?$dataClient['ZipCode']:'' ?>">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label required" for="inputEmail"><?= $inputEmail ?></label>
            <div class="controls">
                <input type="email" name="Email" id="inputEmail" placeholder="" required value="<?= isset($dataClient['Email'])?$dataClient['Email']:'' ?>" <?=isset($dataClient['Email'])?'disabled':'' ?>>
                <span class="help-inline" id="error-email"></span>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label required" for="inputPhone"><?= $inputPhone ?></label>
            <div class="controls">
                <input type="tel" name="Phone" id="inputPhone" placeholder="" required value="<?= isset($dataClient['Phone'])?$dataClient['Phone']:'' ?>">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="inputFax"><?= $inputFax ?></label>
            <div class="controls">
                <input type="tel" name="Fax" id="inputFax" placeholder="" value="<?= isset($dataClient['Fax'])?$dataClient['Fax']:'' ?>">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="inputMobile"><?= $inputMobile ?></label>
            <div class="controls">
                <input type="tel" name="Mobile" id="inputMobile" placeholder="" value="<?= isset($dataClient['Mobile'])?$dataClient['Mobile']:'' ?>">
            </div>
        </div>
        <div class="form-actions">
            <input type="submit" class="btn btn-primary" value="Register">
            <button type="button" class="btn">Cancel</button>
        </div>
    </form>
</div>
<script>
    $(function () {
        $("#inputProvince").val(1);
        $("input,select,textarea").not("[type=submit]").jqBootstrapValidation();
        $('#CustomerForm').submit(function(){
            $('input[required]').each(function(){
                if($(this).val() == ''){
                    $(this).focus();
                    return false;
                }
            });
            return checkDupEmailCustomer();
        });
        
        $("#inputEmail").blur(checkDupEmailCustomer);
        
    });
    function checkDupEmailCustomer(){
        var clientID = $('#ClientID').val();
        if(clientID == ''){
            var isDup = true;
            var email = $("#inputEmail").val();
            if(email != ''){
                $.ajax({
                type: "POST",
                url: 'index.php?r=ClientManage/CheckEmail',
                async: false,
                data: {
                    Email: email
                },
                success:
                    function(result) {
                        if (result==0){
                            isDup = false;
                        }
                    },
                error:
                    function(errorThrown) {
                        alert("Error occured: " + errorThrown);
                    }
                });

                if(isDup){
                    $('#inputEmail').parent().parent().addClass('error');
                    $('#error-email').text('This email used');
                }else{
                    $('#inputEmail').parent().parent().removeClass('error');
                    $('#error-email').text('');
                }

                return !isDup;
            }
        }else{
            return true;
        }
    }
</script>